import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:signage/app/modules/device/controllers/device_controller.dart';
import 'package:signage/app/services/bluetooth_service.dart';
import 'package:signage/app/utils/toast_utils.dart';
import 'package:signage/l10n/l10n.dart';

class HomeController extends GetxController {
  //TODO: Implement HomeController
  final BluetoothManager bluetoothManager = Get.find<BluetoothManager>();

  final count = 0.obs;
  final selectedIndex = 0.obs;

  @override
  void onInit() async {
    super.onInit();
    // 延迟一小段时间再初始化蓝牙
    await Future.delayed(const Duration(milliseconds: 2000));
    FlutterBluePlus.setLogLevel(LogLevel.verbose, color: false);
    await bluetoothManager.initializeCachedDevices();
    await bluetoothManager.checkPermissionAndStartScan();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  void changeIndex(int index) {
    selectedIndex.value = index;
    if (index == 1) {
      ToastUtils.show(
          AppLocalizations.of(Get.context!)!.translate('device_update_status'));
      bluetoothManager.checkPermissionAndStartScan();
      Get.find<DeviceController>().onResumed();
    }
  }
}
